﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Data;
using BookStore.Bussiness;

namespace BookStore.admin
{
    public partial class FAQ : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll()
        {
            gvFAQ.DataSource = FAQService.getAll();
            gvFAQ.DataBind();
        }
        public void ResetControl()
        {
            txtfaqID.Text ="";
            txtfaqName.Text ="";
            txtContent.Text = "";
        }
        public void insertFAQ()
        {
            FAQInfo objfaq = new FAQInfo();
            objfaq.faqName = txtfaqName.Text;
            objfaq.faqContent = txtContent.Text;
            bool test = FAQService.insertFAQ(objfaq);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateFAQ()
        {
            FAQInfo objfaq = new FAQInfo();
            objfaq.faqID = Convert.ToInt32(txtfaqID.Text);
            objfaq.faqName = txtfaqName.Text;
            objfaq.faqContent = txtContent.Text;
            bool test = FAQService.updateFAQ(objfaq);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }
        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            ResetControl();
            pnView.Visible = false;
            pnUpdate.Visible = true;
        }

        protected void lbtRefreshB_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void lbtBackT_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            if (flag==true)
            {
                insertFAQ();
            }
            else
            {
                updateFAQ();
            }
            showAll();
            pnUpdate.Visible = false;
            pnView.Visible = true;
        }

        protected void gvFAQ_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int faqID = Convert.ToInt32(gvFAQ.Rows[e.RowIndex].Cells[0].Text);
            bool test = FAQService.deleteFAQ(faqID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvFAQ_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag=false;
            int faqID = Convert.ToInt32(gvFAQ.Rows[e.NewSelectedIndex].Cells[0].Text);
            FAQInfo objFAQ = FAQService.getbyID(faqID);
            txtfaqID.Text = objFAQ.faqID.ToString();
            txtfaqName.Text = objFAQ.faqName;
            txtContent.Text = objFAQ.faqContent;
            pnUpdate.Visible = true;
            pnView.Visible = false;
        }

        protected void gvFAQ_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvFAQ.PageIndex = e.NewPageIndex;
            showAll();
        }
    }
}
